package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.venueservice.dto.ServiceLossDTO;
import com.eastfair.venueservice.dto.ServiceLossPageQuery;
import com.eastfair.venueservice.dto.ServiceLossSaveDTO;
import com.eastfair.venueservice.dto.ServiceLossUpdateDTO;
import com.eastfair.venueservice.entity.ServiceLoss;
import com.eastfair.venueservice.service.ServiceLossService;
import com.eastfair.venueservice.vo.ServiceLossVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 报损记录
 * </p>
 *
 * @author linan
 * @date 2022-09-19
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/serviceLoss")
@Api(value = "ServiceLoss", tags = "报损记录")
@PreAuth(replace = "venueservice:serviceLoss:")
public class ServiceLossController extends SuperController<ServiceLossService, Long, ServiceLoss, ServiceLossPageQuery, ServiceLossSaveDTO, ServiceLossUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ServiceLossService serviceLossService;

    @Override
    public void handlerResult(IPage<ServiceLoss> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ServiceLoss> serviceLossList = list
                .stream()
                .map((map) -> ServiceLoss.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(serviceLossList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ServiceLossPageQuery> params) {
        ServiceLossPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页查询报损记录VO列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询报损记录VO列表")
    @PostMapping("/query/serviceLoss")
    public R<Page<ServiceLossVO>> queryServiceLoss(@RequestBody PageParams<ServiceLossDTO> pageParams) {
        log.info("queryServiceLoss - 分页查询报损记录VO列表, pageParams={}", pageParams);
        try {
            return R.successDef(serviceLossService.listServiceLossVO(pageParams));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 返修入库.
     *
     * @param serviceLossDTO the service loss dto
     * @return the r
     */
    @ApiOperation("返修入库")
    @SysLog("返修入库")
    @PostMapping("/repair/inInventory")
    public R<Boolean> repairInInventory(@RequestBody ServiceLossDTO serviceLossDTO) {
        log.info("repairInInventory - 返修入库, serviceLossDTO={}", serviceLossDTO);
        try {
            return R.successDef(serviceLossService.repairInInventory(serviceLossDTO));
        } catch (BizException e) {
            return R.fail(e);
        }
    }

    /**
     * 报损类型统计.
     *
     * @param serviceLossDTO the service loss dto
     * @return the r
     */
    @ApiOperation("报损类型统计")
    @PostMapping("/count/lossNum")
    public R<Map<String, Object>> countLossNum(@RequestBody ServiceLossDTO serviceLossDTO) {
        log.info("countLossNum - 报损类型统计, serviceLossDTO={}", serviceLossDTO);
        try {
            Map<String, Object> map = serviceLossService.countLossNumByGoodsLossState(serviceLossDTO);
            return R.successDef(map);
        } catch (BizException e) {
            return R.fail(e);
        }
    }
}
